package com.wdz.pma.dao;

import com.wdz.pma.dto.StudentProject;
import com.wdz.pma.entities.Project;
import com.wdz.pma.entities.Student;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository         //继承CrudRepository
public interface StudentRepository extends CrudRepository<Student, Long> {

    //覆盖原本默认的findAll()方法，原先返回-Iterable,这里自定义返回-List【列表】
    //    列表实体为Student
//    @Override
//    public List<Student> findAll();

//  ctrl+O 生成覆盖方法findAll，将Iterable改为List
    @Override
    List<Student> findAll();

    @Query(nativeQuery = true, value = "SELECT s.name, s.wechat_id AS wechatId, COUNT(s.student_id) AS projectCount " +
            "FROM student AS s LEFT JOIN project_student AS ps ON s.student_id = ps.student_id " +
            "GROUP BY s.name, s.wechat_id ORDER BY 3 desc;")
   List<StudentProject> studentProjects();

    Student findByStudentId(long theId);
//    定义这个方法前，先引入：import com.wdz.pma.dto.StudentProject;
}


